<template>
	<view class="device-content">
		<view class="opts">
			<view class="btn" @click="scan">
				<image src="../../../../static/images/icon/scan.png"></image>
				<text>{{$t('baseCommon.buttons.scan')}}</text>
			</view>
			<view class="btn" @click="nfc">
				<image src="../../../../static/images/icon/NFC.png"></image>
				<text>NFC</text>
			</view>
		</view>
		<view class="context">
			<view class="desc">
				{{ $t('equipment.common.locaNo') }}：{{ deviceInfo.locaNo }}
			</view>
			<view class="desc">
				{{ $t('equipment.common.locaName') }}：{{ deviceInfo.locaName }}
			</view>
		</view>
		<view class="buttombtn">
			<view>
				<u-button type="primary" @click="doSure">确定</u-button>
			</view>
			<view>
				<u-button type="primary" @click="doCancel">取消</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import { notice } from '@/utils/common.js'
	import NFCMixin from '@/utils/business/businessNFC.js'
	
	export default {
		mixins: [
			NFCMixin
		],
		data() {
			return {
				deviceInfo: {
					locaId: '',
					locaNo: '',
					locaName: ''
				},
				nfcData: ''
			}
		},
		watch: {
			nfcData: {
				handler(val) {
					this.handleNfc()
				},
				// immediate: true
			}
		},
		onLoad(option) {
			// #ifdef APP-PLUS
			this.listenNFCStatus()
			this.readyRead = true
			// #endif
		},
		methods: {
			scan() {
				let that = this
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						let data = business.handleCode(res.result)
						let scanType = data.scanType
						let scanId = data.scanId
						let scanNo = data.scanNo
						let scanName = data.scanName
						
						if(scanType == 'KKS') {
							that.deviceInfo.locaId = scanId
							that.deviceInfo.locaNo = scanNo
							that.deviceInfo.locaName = scanName
						} else {
							notice.toast('请扫描设备二维码')
						}
					}
				})
			},
			nfc() {
				notice.toast(this.$t('inspection.taskExecute.nfcRead'))
			},
			handleNfc() {
				if(this.nfcData) {
					let data = business.handleCode(this.nfcData)
					let scanType = data.scanType
					let scanId = data.scanId
					let scanNo = data.scanNo
					let scanName = data.scanName
					
					if(scanType == 'KKS') {
						this.deviceInfo.locaId = scanId
						this.deviceInfo.locaNo = scanNo
						this.deviceInfo.locaName = scanName
					} else {
						notice.toast('请读取设备NFC数据')
						this.nfcData = ''
					}
				}
			},
			doSure() {
				const eventChannel = this.getOpenerEventChannel();
				eventChannel.emit('selectLoca', this.deviceInfo);
				uni.navigateBack()
			},
			doCancel() {
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.device-content {
		padding-top: 10px;
	}
	
	.opts {
		display: flex;
		margin: 0 5px;
		
		.btn {
			display: flex;
			align-items: center;
			border: 1px solid #1e91e6;
			padding: 5px 15px;
			margin: 0 5px;
			background-color: #1e91e6;
			border-radius: 5px;
		}
		
		image {
			width: 20px;
			height: 20px;
			margin-right: 5px;
		}
		
		text {
			font-size: 16px;
			color: #fff
		}
	}
	
	.context {
		background-color: #fff;
		margin-top: 10px;
	}
	
	.desc {
		padding: 10px;
		border-bottom: 1px solid #eee;
	}
	
	.buttombtn {
		display: flex;
		position: fixed;
		bottom: 5px;
		width: 96%;
		left: 2%;
	}
	
	.buttombtn>view {
		flex: 1;
		padding: 0 10upx;
	}
</style>